package io.github.talelin.latticy.controller.v1;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.ActivityDTO;
import io.github.talelin.latticy.dto.query.BasePageDTO;
import io.github.talelin.latticy.model.Activity;
import io.github.talelin.latticy.model.ActivityDetail;
import io.github.talelin.latticy.service.ActivityService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;

/**
 * @author tanrentao
 * @since 2021-08-10
 * @decs 活动控制器
 */
@Validated
@RestController
@RequestMapping("/v1/activity")
@PermissionModule(value = "活动")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 创建活动
     * @param dto
     */
    @PostMapping("")
    @PermissionMeta("创建活动")
    @GroupRequired
    public CreatedVO create(@RequestBody @Validated ActivityDTO dto) {
        activityService.create(dto);
        return new CreatedVO();
    }

    /**
     * 更新活动
     * @param dto
     * @param id
     */
    @PutMapping("/{id}")
    @PermissionMeta("更新活动")
    @GroupRequired
    public UpdatedVO update(@RequestBody @Validated ActivityDTO dto,
                            @PathVariable @Positive(message = "{id.positive}") Integer id) {
        activityService.update(dto, id);
        return new UpdatedVO();
    }

    /**
     * 删除活动
     * @param id
     */
    @DeleteMapping("/{id}")
    @PermissionMeta("删除活动")
    @GroupRequired
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        Activity activity = activityService.getById(id);
        if (activity == null) {
            throw new NotFoundException(90000);
        }
        activityService.getBaseMapper().deleteById(id);
        return new DeletedVO();
    }

    /**
     * 根据id查询活动详情信息
     * @param id
     */
    @GetMapping("/{id}/detail")
    @LoginRequired
    public ActivityDetail get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        ActivityDetail activityDetail = activityService.getDetailById(id);
        return activityDetail;
    }

    /**
     * 活动分页查询
     * @param dto
     */
//    @GetMapping("/page")
//    @LoginRequired
//    public PageResponseVO<Activity> page( @RequestParam(name = "count", required = false, defaultValue = "10")
//                                          @Min(value = 1, message = "{page.count.min}")
//                                          @Max(value = 30, message = "{page.count.max}") Integer count,
//                                          @RequestParam(name = "page", required = false, defaultValue = "0")
//                                          @Min(value = 0, message = "{page.number.min}") Integer page) {
//        Page<Activity> pager = new Page<>(page, count);
//        IPage<Activity> paging = activityService.getBaseMapper().selectPage(pager, null);
//        return PageUtil.build(paging);
//    }
    @GetMapping("/page")
    @LoginRequired
    public PageResponseVO<Activity> page(@Validated BasePageDTO dto) {
        Page<Activity> pager = new Page<>(dto.getPage(), dto.getCount());
        IPage<Activity> paging = activityService.getBaseMapper().selectPage(pager, null);
        return PageUtil.build(paging);
    }




}
